//
//  NSECGRealTimeView.m
//  SpeedTest
//
//  Created by Han Mingjie on 2020/2/10.
//  Copyright © 2020 MingJie Han. All rights reserved.
//

#import "UIECGRecordingView.h"
#import "NSECGRealTimeController.h"
#import "NSECGLinesView.h"
#import "UIECGBackgroundView.h"
#define ECG_LINES_VIEW_NUM 5


@interface UIECGRecordingView()<NSECGRealTimeViewDelegate>{
    NSECGRealTimeController *ecg_controller;
    NSECGLinesView *ecg_lineview[ECG_LINES_VIEW_NUM];
    UIECGBackgroundView *background_view;
    NSThread *control_thread;
    NSDate *draw_start_date_time;
    NSUInteger more_point_per_side;     //每边多出一些点，用于防止锯齿
}
@end

@implementation UIECGRecordingView
@synthesize delegate;
@synthesize ecg_name_array;
@synthesize ecg_lead_status;
@synthesize style;
@synthesize standard_style;
@synthesize x_speed,y_rate,view_fresh,ECG_rate,pix_per_mm,space_pix,ecg_line_width;
@synthesize space6x2PerRow,space3x4PerRow;
@synthesize ecg_line_color;
@synthesize standard_text_color,standard_text_font;
@synthesize lost_line_color;
@synthesize bg5mmWidth,bg5mmColor;
@synthesize bg1mmWidth,bg1mmColor,background_color;
@synthesize bg10mmColor,bg10mmWidth;
@synthesize long_lead_name;


-(void)changeTo12x1{
    CGRect rect = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
#if TARGET_OS_IOS
    [ecg_lineview[0] setFrame:rect];
    [ecg_lineview[1] setFrame:CGRectZero];
    [ecg_lineview[2] setFrame:CGRectZero];
    [ecg_lineview[3] setFrame:CGRectZero];
    [ecg_lineview[4] setFrame:CGRectZero];
#else
    [ecg_lineview[0] setFrame:NSRectFromCGRect(rect)];
    [ecg_lineview[1] setFrame:NSRectFromCGRect(CGRectZero)];
    [ecg_lineview[2] setFrame:NSRectFromCGRect(CGRectZero)];
    [ecg_lineview[3] setFrame:NSRectFromCGRect(CGRectZero)];
    [ecg_lineview[4] setFrame:NSRectFromCGRect(CGRectZero)];
#endif
    ecg_lineview[0].ecg_name_array = ecg_name_array;
    ecg_lineview[1].ecg_name_array = nil;
    ecg_lineview[2].ecg_name_array = nil;
    ecg_lineview[3].ecg_name_array = nil;
    ecg_lineview[4].ecg_name_array = nil;
    return;
}

-(void)changeTo6x2{
#if TARGET_OS_IOS
    CGFloat height = self.frame.size.height *7.f/8.f;
    float width = (self.frame.size.width - space6x2PerRow * pix_per_mm)/2.f;
    CGRect rect1 = CGRectMake(0.f, 0.f, width, height);
    CGRect rect2 = CGRectMake(width + space6x2PerRow * pix_per_mm, 0.f, width, height);
    CGRect rect3 = CGRectMake(0.f, self.frame.size.height * 6.f/8.f, self.frame.size.width, self.frame.size.height * 2.f /8.f);
    [ecg_lineview[0] setFrame:rect1];
    [self bringSubviewToFront:ecg_lineview[0]];
    [ecg_lineview[1] setFrame:rect2];
    [self bringSubviewToFront:ecg_lineview[1]];
    [ecg_lineview[2] setFrame:rect3];
    [ecg_lineview[3] setFrame:CGRectZero];
    [ecg_lineview[4] setFrame:CGRectZero];
#else
    CGFloat height = self.frame.size.height *7.f/8.f;
    CGRect rect1 = CGRectMake(0.f, self.frame.size.height - height, self.frame.size.width/2.f, height);
    CGRect rect2 = CGRectMake(self.frame.size.width/2.f, self.frame.size.height - height, self.frame.size.width/2.f, height);
    CGRect rect3 = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height * 2.f /8.f);
    [ecg_lineview[0] setFrame:NSRectFromCGRect(rect1)];
    [ecg_lineview[1] setFrame:NSRectFromCGRect(rect2)];
    [ecg_lineview[2] setFrame:NSRectFromCGRect(rect3)];
    [ecg_lineview[3] setFrame:NSRectFromCGRect(CGRectZero)];
    [ecg_lineview[4] setFrame:NSRectFromCGRect(CGRectZero)];
#endif
    ecg_lineview[0].ecg_name_array = @[@"I",@"II",@"III",@"aVR",@"aVL",@"aVF"];
    ecg_lineview[1].ecg_name_array = @[@"V1",@"V2",@"V3",@"V4",@"V5",@"V6"];;
    ecg_lineview[2].ecg_name_array = @[long_lead_name];
    ecg_lineview[3].ecg_name_array = nil;
    ecg_lineview[4].ecg_name_array = nil;
}

-(void)changeTo3x4WithLandScape{
    #if TARGET_OS_IOS
        float extSpaceForOtherRow = 2.5f * pix_per_mm;  //except first row.
        if (NSECGRealTimeView_Standard_Show == standard_style){
            extSpaceForOtherRow = 0.f;
        }
        float view_width = (self.frame.size.width - 3 * space3x4PerRow * pix_per_mm - 3.f * extSpaceForOtherRow)/ 4.f;
        float view_height = self.frame.size.height * 4.f/5.f;
        
        CGRect rect0 = CGRectMake(0.f, 0.f, view_width, view_height);                      //Row 1
        CGRect rect1 = CGRectMake(space3x4PerRow * pix_per_mm + view_width, 0.f,
                                  view_width + extSpaceForOtherRow, view_height);          //Row2
        CGRect rect2 = CGRectMake(2 * (space3x4PerRow * pix_per_mm + view_width), 0.f,
                                  view_width + extSpaceForOtherRow, view_height);           //Row3
        CGRect rect3 = CGRectMake(3 * (space3x4PerRow * pix_per_mm + view_width), 0.f,
                                  view_width + extSpaceForOtherRow, view_height);           //Row4
        
        //Long lead view.
        CGRect rect4 = CGRectMake(0.f, self.frame.size.height * 3.f/5.f, self.frame.size.width, self.frame.size.height * 2.f/5.f);
        
        [ecg_lineview[0] setFrame:rect0];
//    ecg_lineview[0].backgroundColor = [UIColor redColor];
        [self bringSubviewToFront:ecg_lineview[0]];
        [ecg_lineview[1] setFrame:rect1];
//    ecg_lineview[1].backgroundColor = [UIColor blueColor];
        [self bringSubviewToFront:ecg_lineview[1]];
        [ecg_lineview[2] setFrame:rect2];
        [self bringSubviewToFront:ecg_lineview[2]];
        [ecg_lineview[3] setFrame:rect3];
        [self bringSubviewToFront:ecg_lineview[3]];
        [ecg_lineview[4] setFrame:rect4];
    #else
        float view_width = self.frame.size.width / 4.f;
        float view_height = self.frame.size.height *4.f/5.f;
        float view_seek_y = self.frame.size.height - view_height;

        CGRect rect0 = CGRectMake(0.f, view_seek_y, view_width, view_height);
        CGRect rect1 = CGRectMake(1.f * view_width, view_seek_y, view_width, view_height);
        CGRect rect2 = CGRectMake(2.f * view_width, view_seek_y, view_width, view_height);
        CGRect rect3 = CGRectMake(3.f * view_width, view_seek_y, view_width, view_height);
        CGRect rect4 = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height * 2.f/5.f);
        
        [ecg_lineview[0] setFrame:NSRectFromCGRect(rect0)];
        [ecg_lineview[1] setFrame:NSRectFromCGRect(rect1)];
        [ecg_lineview[2] setFrame:NSRectFromCGRect(rect2)];
        [ecg_lineview[3] setFrame:NSRectFromCGRect(rect3)];
        [ecg_lineview[4] setFrame:NSRectFromCGRect(rect4)];
    #endif
        ecg_lineview[0].ecg_name_array = @[@"I",@"II",@"III"];
        ecg_lineview[1].ecg_name_array = @[@"aVR",@"aVL",@"aVF"];
        ecg_lineview[2].ecg_name_array = @[@"V1",@"V2",@"V3"];
        ecg_lineview[3].ecg_name_array = @[@"V4",@"V5",@"V6"];
        ecg_lineview[4].ecg_name_array = @[long_lead_name];              //长导联
}

-(void)changeTo3x4WithPortrait{
    #if TARGET_OS_IOS
        float view_width = self.frame.size.width / 3.f;
        float view_height = self.frame.size.height * 5.f/6.f;

        CGRect rect0 = CGRectMake(0.f, 0.f, view_width, view_height);
        CGRect rect1 = CGRectMake(view_width, 0.f, view_width, view_height);
        CGRect rect2 = CGRectMake(2 * view_width, 0.f, view_width, view_height);
        CGRect rect3 = CGRectZero;
        CGRect rect4 = CGRectMake(0.f, self.frame.size.height * 4.f/6.f, self.frame.size.width, self.frame.size.height * 2.f/6.f);
        
        [ecg_lineview[0] setFrame:rect0];
        [self bringSubviewToFront:ecg_lineview[0]];
        [ecg_lineview[1] setFrame:rect1];
        [self bringSubviewToFront:ecg_lineview[1]];
        [ecg_lineview[2] setFrame:rect2];
        [self bringSubviewToFront:ecg_lineview[2]];
        [ecg_lineview[3] setFrame:rect3];
        [ecg_lineview[4] setFrame:rect4];
    #else
        float view_width = self.frame.size.width / 3.f;
        float view_height = self.frame.size.height *5.f/6.f;
        float view_seek_y = self.frame.size.height - view_height;

        CGRect rect0 = CGRectMake(0.f, view_seek_y, view_width, view_height);
        CGRect rect1 = CGRectMake(1.f * view_width, view_seek_y, view_width, view_height);
        CGRect rect2 = CGRectMake(2.f * view_width, view_seek_y, view_width, view_height);
        CGRect rect3 = CGRectZero;
        CGRect rect4 = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height * 2.f/6.f);
        
        [ecg_lineview[0] setFrame:NSRectFromCGRect(rect0)];
        [ecg_lineview[1] setFrame:NSRectFromCGRect(rect1)];
        [ecg_lineview[2] setFrame:NSRectFromCGRect(rect2)];
        [ecg_lineview[3] setFrame:NSRectFromCGRect(rect3)];
        [ecg_lineview[4] setFrame:NSRectFromCGRect(rect4)];
    #endif
        ecg_lineview[0].ecg_name_array = @[@"I",@"aVR",@"V1",@"V4"];
        ecg_lineview[1].ecg_name_array = @[@"II",@"aVL",@"V2",@"V5"];
        ecg_lineview[2].ecg_name_array = @[@"III",@"aVF",@"V3",@"V6"];
        ecg_lineview[3].ecg_name_array = nil;
        ecg_lineview[4].ecg_name_array = @[long_lead_name];              //长导联
}

-(void)changeTo3x4{
    if (self.frame.size.width > self.frame.size.height){
        [self changeTo3x4WithLandScape];
    }else{
        [self changeTo3x4WithPortrait];
    }
}

-(void)changeTo6x1{
    CGRect rect0 = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
#if TARGET_OS_IOS
    [ecg_lineview[0] setFrame:rect0];
    [ecg_lineview[1] setFrame:CGRectZero];
    [ecg_lineview[2] setFrame:CGRectZero];
    [ecg_lineview[3] setFrame:CGRectZero];
    [ecg_lineview[4] setFrame:CGRectZero];
#else
    [ecg_lineview[0] setFrame:NSRectFromCGRect(rect0)];
    [ecg_lineview[1] setFrame:NSRectFromCGRect(CGRectZero)];
    [ecg_lineview[2] setFrame:NSRectFromCGRect(CGRectZero)];
    [ecg_lineview[3] setFrame:NSRectFromCGRect(CGRectZero)];
    [ecg_lineview[4] setFrame:NSRectFromCGRect(CGRectZero)];
#endif
    ecg_lineview[0].ecg_name_array = @[@"I",@"II",@"III",@"aVR",@"aVL",@"aVF"];
    ecg_lineview[1].ecg_name_array = nil;
    ecg_lineview[2].ecg_name_array = nil;
    ecg_lineview[3].ecg_name_array = nil;
    ecg_lineview[4].ecg_name_array = nil;
}

-(void)view_size_changed{
    CGRect rect = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
#if TARGET_OS_IOS
    [background_view setFrame:rect];
#else
    [background_view setFrame:NSRectFromCGRect(rect)];
#endif
    switch (style) {
        case NSECGRealTimeView_Style_12x1:
            [self changeTo12x1];
            break;
        case NSECGRealTimeView_Style_6x2:
            [self changeTo6x2];
            break;
        case NSECGRealTimeView_Style_3x4:
            [self changeTo3x4];
            break;
        case NSECGRealTimeView_Style_limb_leads:
            [self changeTo6x1];
            break;
        default:
            break;
    }
}

-(void)reset_all{
    style = NSECGRealTimeView_Style_3x4;
    standard_style = NSECGRealTimeView_Standard_Auto;
    more_point_per_side = 10;
    ecg_name_array = @[@"I",@"II",@"III",@"aVR",@"aVL",@"aVF",@"V1",@"V2",@"V3",@"V4",@"V5",@"V6"];
    long_lead_name = @"II";
    x_speed = 25.f;
    y_rate = 10.f;
    view_fresh = 25.f;
    ECG_rate = 660.f;
    pix_per_mm = 5.f;
    space_pix = 2.f;
    space3x4PerRow = 0.f;
    space6x2PerRow = 0.f;
#if TARGET_OS_IOS
    ecg_line_color = [UIColor greenColor];
    bg5mmColor = [UIColor darkGrayColor];
    bg1mmColor = [UIColor darkGrayColor];
    background_color = [UIColor blackColor];
    standard_text_font = [UIFont systemFontOfSize:12.f];
    standard_text_color = [UIColor greenColor];
#else
    ecg_line_color = [NSColor greenColor];
    background_line_color = [NSColor darkGrayColor];
    background_weak_line_color = [NSColor darkGrayColor];
    background_color = [NSColor blackColor];
    standard_text_font = [NSFont systemFontOfSize:12.f];
    standard_text_color = [NSColor greenColor];
#endif
    ecg_line_width = 1.6f;
    lost_line_color = [UIColor redColor];
    bg5mmWidth = 0.8f;
    bg1mmWidth = 0.2f;
    
    if (nil == background_view){
        CGRect rect = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
#if TARGET_OS_IOS
        background_view = [[UIECGBackgroundView alloc] initWithFrame:rect];
#else
        background_view = [[NSECGBackgroundView alloc] initWithFrame:NSRectFromCGRect(rect)];
#endif
        background_view.pixPermm = pix_per_mm;
        background_view.background_color = background_color;
        background_view.bg10mmColor = bg10mmColor;
        background_view.bg10mmWidth = bg10mmWidth;
        background_view.bg5mmColor = bg5mmColor;
        background_view.bg5mmWidth = bg5mmWidth;
        background_view.bg1mmColor = bg1mmColor;
        background_view.bg1mmWidth = bg1mmWidth;
        [self addSubview:background_view];
    }
    
    ecg_controller = [[NSECGRealTimeController alloc] init];
    ecg_controller.ecg_name_array = ecg_name_array;
    ecg_controller.number_for_maximum_count = 5000;
    
    
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i] = [[NSECGLinesView alloc] init];
//        ecg_lineview[i].tag = i;
        ecg_lineview[i].ecg_controller = ecg_controller;
        ecg_lineview[i].x_speed = x_speed;
        ecg_lineview[i].y_rate = y_rate;
        ecg_lineview[i].view_fresh = view_fresh;
        ecg_lineview[i].ECG_rate = ECG_rate;
        ecg_lineview[i].pix_per_mm = pix_per_mm;
        ecg_lineview[i].space_pix = space_pix * pix_per_mm;
        ecg_lineview[i].line_width = ecg_line_width;
        ecg_lineview[i].line_color = ecg_line_color;
        ecg_lineview[i].lost_line_color = lost_line_color;
        ecg_lineview[i].standard_text_color = standard_text_color;
        ecg_lineview[i].standard_text_font = standard_text_font;
        ecg_lineview[i].standard_style = standard_style;
        ecg_lineview[i].style = style;
        ecg_lineview[i].delegate = self;
        [self addSubview:ecg_lineview[i]];
    }
    
    //Debug Start
//    ecg_lineview[0].backgroundColor = [UIColor blueColor];
//    ecg_lineview[1].backgroundColor = [UIColor yellowColor];
//    ecg_lineview[2].backgroundColor = [UIColor purpleColor];
//    ecg_lineview[3].backgroundColor = [UIColor redColor];
//    ecg_lineview[4].backgroundColor = [UIColor orangeColor];
    //Debug End
    
    
    if (control_thread){
        return;
    }
    control_thread = [[NSThread alloc] initWithTarget:self selector:@selector(control_thread) object:nil];
    control_thread.name = @"Control Play Thread.";
    draw_start_date_time = [NSDate dateWithTimeInterval:-0.5 sinceDate:[NSDate date]];
    [control_thread start];
//    [control_thread performSelector:@selector(start) withObject:nil afterDelay:0.5];
}

-(BOOL)add_realtime_ECG_in_thread:(NSArray <NSArray <NSNumber *>*>* )ecg_array{
    return [ecg_controller add_realtime_ECG_in_thread:ecg_array];
}

-(BOOL)cleanControlCache{
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        if (ecg_lineview[i]){
            [ecg_lineview[i] cancelAllRefresh];
        }
    }
    [ecg_controller empty_all_ECG];
    return YES;
}

-(void)control_thread{
    NSUInteger run_time = 0;
    float move_point = 0.f;
    while (YES) {
        run_time ++;
        NSDate *last_date = [NSDate date];
        BOOL drawed = NO;
        BOOL enought_data = [ecg_controller enough_point_for_next_piece:2 * more_point_per_side + ECG_rate/view_fresh];
        if (enought_data){
            drawed = YES;
            NSUInteger index = ecg_controller.drawed_ecg_index;
            for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
                if (ecg_lineview[i].self_width > 0){
                    [ecg_lineview[i] fresh_in_threadWithIndex:index];
                }
            }
            move_point += ECG_rate/view_fresh;
            NSUInteger remove_points_this_time = (NSUInteger)move_point;
            move_point -= remove_points_this_time;
            [ecg_controller one_piece_completed:remove_points_this_time];
        }
        
        if ( (ecg_controller.last_add_ecg_index > ecg_controller.drawed_ecg_index)
            && ecg_controller.last_add_ecg_index - ecg_controller.drawed_ecg_index > ECG_rate){
            //倍速画下一个PieceView
            [NSThread sleepUntilDate:[NSDate dateWithTimeInterval:0.5f/view_fresh sinceDate:last_date]];
        }else{
            //待画长度小于1秒，无需加速；
            [NSThread sleepUntilDate:[NSDate dateWithTimeInterval:1.f/view_fresh sinceDate:last_date]];
        }
    }
}


#pragma mark - Property Set
-(void)setEcg_name_array:(NSArray *)_ecg_name_array{
    ecg_name_array = _ecg_name_array;
    ecg_controller.ecg_name_array = ecg_name_array;
}

-(void)setEcg_lead_status:(NSArray<NSNumber *> *)_ecg_lead_status{
    ecg_lead_status = _ecg_lead_status;
    ecg_controller.ecg_lead_status = ecg_lead_status;
}

-(void)setStyle:(NSECGRealTimeView_Style)_style{
    style = _style;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].style = style;
    }
    draw_start_date_time = [NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]];
    [self view_size_changed];
}

-(void)setX_speed:(float)_x_speed{
    x_speed = _x_speed;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].x_speed = x_speed;
    }
    draw_start_date_time = [NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]];
}

-(void)setY_rate:(float)_y_rate{
    y_rate = _y_rate;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].y_rate = y_rate;
    }
    draw_start_date_time = [NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]];
}

-(void)setView_fresh:(float)_view_fresh{
    view_fresh = _view_fresh;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].view_fresh = view_fresh;
    }
    draw_start_date_time = [NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]];
}

-(void)setECG_rate:(float)_ECG_rate{
    ECG_rate = _ECG_rate;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].ECG_rate = ECG_rate;
    }
    draw_start_date_time = [NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]];
}

-(void)setPix_per_mm:(float)_pix_per_mm{
    pix_per_mm = _pix_per_mm;
    background_view.pixPermm = pix_per_mm;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].pix_per_mm = pix_per_mm;
    }
    draw_start_date_time = [NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]];
}

-(void)setSpace_pix:(NSUInteger)_space_pix{
    space_pix = _space_pix;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].space_pix = space_pix * pix_per_mm;
    }
    draw_start_date_time = [NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]];
}

-(void)setEcg_line_width:(float)_ecg_line_width{
    ecg_line_width = _ecg_line_width;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].line_width = ecg_line_width;
    }
}

-(void)setStandard_style:(NSECGRealTimeView_Standard)_standard_style{
    standard_style = _standard_style;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].standard_style = standard_style;
    }
    [self view_size_changed];
}

-(void)setLong_lead_name:(NSString *)_long_lead_name{
    if ([long_lead_name isEqualToString:_long_lead_name]){
        return;
    }
    long_lead_name = _long_lead_name;
    if (NSECGRealTimeView_Style_6x2 == style){
        ecg_lineview[2].ecg_name_array = @[long_lead_name];
    }
    if (NSECGRealTimeView_Style_3x4 == style){
        ecg_lineview[4].ecg_name_array = @[long_lead_name];
    }
}

-(void)setLost_line_color:(UIColor *)_lost_line_color{
    lost_line_color = _lost_line_color;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].lost_line_color = lost_line_color;
    }
}

-(void)setEcg_line_color:(UIColor *)_ecg_line_color{
    ecg_line_color = _ecg_line_color;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].line_color = ecg_line_color;
    }
}

-(void)setStandard_text_color:(UIColor *)_standard_text_color{
    standard_text_color = _standard_text_color;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].standard_text_color = standard_text_color;
    }
}

-(void)setStandard_text_font:(UIFont *)_standard_text_font{
    standard_text_font = _standard_text_font;
    for (int i=0;i<ECG_LINES_VIEW_NUM;i++){
        ecg_lineview[i].standard_text_font = standard_text_font;
    }
}
        
-(void)setBg10mmWidth:(float)_background_board_line_width{
    bg10mmWidth = _background_board_line_width;
    background_view.bg10mmWidth = bg10mmWidth;
}

-(void)setBg10mmColor:(UIColor *)_background_board_line_color{
    bg10mmColor = _background_board_line_color;
    background_view.bg10mmColor = bg10mmColor;
}

-(void)setBackground_color:(UIColor *)_background_color{
    background_color = _background_color;
    background_view.background_color = background_color;
}
    
-(void)setBg5mmWidth:(float)_background_line_width{
    bg5mmWidth = _background_line_width;
    background_view.bg5mmWidth = bg5mmWidth;
}

-(void)setBg5mmColor:(UIColor *)_background_line_color{
    bg5mmColor = _background_line_color;
    background_view.bg5mmColor = bg5mmColor;
}

-(void)setBg1mmColor:(UIColor *)_background_small_line_color{
    bg1mmColor = _background_small_line_color;
    background_view.bg1mmColor = bg1mmColor;
}

-(void)setBg1mmWidth:(float)_background_small_line_width{
    bg1mmWidth = _background_small_line_width;
    background_view.bg1mmWidth = bg1mmWidth;
}



#pragma mark - View System Start
-(id)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self){
        [self reset_all];
    }
    return self;
}

-(id)init{
    self = [super init];
    if (self){
    }
    return self;
}

-(id)initWithFrame:(CGRect)frameRect{
    self = [super initWithFrame:frameRect];
    if (self){
        [self reset_all];
    }
    return self;
}

#if TARGET_OS_IOS
-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    if (0 == frame.size.width || 0 == frame.size.height){
        return;
    }
    [self view_size_changed];
}
#else
-(void)setFrameSize:(NSSize)newSize{
    [super setFrameSize:newSize];
    [self view_size_changed];
}
#endif
    
#pragma mark - NSECGRealTimeViewDelegate
-(void)clickedLead:(NSString *)lead_name{
    if (delegate){
        [delegate clickedLead:lead_name];
    }
}

@end
